﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.ENT;
namespace KM.DAT
{
    /// <summary>
    /// This is datLoaiTin class
    /// </summary>

    public class datLoaiTin
    {
        public datLoaiTin()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === tạo mới và sửa LoaiTin===
        public void createEdit(entLoaiTin entLT)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiTinCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[8];
            sqla[0] = new SqlParameter("Loai_tin_id", DbType.Int32);
            sqla[1] = new SqlParameter("Ten", DbType.Int32);
            sqla[2] = new SqlParameter("So_tin_hien_thi", DbType.Int32);
            sqla[3] = new SqlParameter("Nguoi_tao", DbType.Int32);
            sqla[4] = new SqlParameter("Ngay_tao", DbType.Int32);
            sqla[5] = new SqlParameter("Nguoi_sua", DbType.Int32);
            sqla[6] = new SqlParameter("Ngay_sua", DbType.Int32);
            sqla[7] = new SqlParameter("Status", DbType.Int32);

            sqla[0].Value = entLT.Loai_tin_id;
            sqla[1].Value = entLT.Ten;
            sqla[2].Value = entLT.So_tin_hien_thi;
            sqla[3].Value = entLT.Nguoi_tao;
            sqla[4].Value = entLT.Ngay_tao;
            sqla[5].Value = entLT.Nguoi_sua;
            sqla[6].Value = entLT.Ngay_sua;
            sqla[7].Value = entLT.Status;


            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall LoaiTin===
        public List<entLoaiTin> getAll()
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiTinGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;

            dr = cmd.ExecuteReader();
            entLoaiTin entLT = new entLoaiTin();
            List<entLoaiTin> lst = new List<entLoaiTin>();
            while (dr.Read())
            {
                entLT = new entLoaiTin();
                entLT.Loai_tin_id = Convert.ToInt32(dr["Loai_tin_id"].ToString());
                entLT.Ten = dr["Ten"].ToString();
                entLT.So_tin_hien_thi = Convert.ToByte(dr["So_tin_hien_thi"].ToString());
                entLT.Nguoi_tao = Convert.ToInt32(dr["Nguoi_tao"].ToString());
                entLT.Ngay_tao = dr["Ngay_tao"].ToString();
                entLT.Nguoi_sua = Convert.ToInt32(dr["Nguoi_sua"].ToString());
                entLT.Ngay_sua = dr["Ngay_sua"].ToString();
                entLT.Status = Convert.ToByte(dr["Status"].ToString());

                lst.Add(entLT);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion

        #region  === getinfo LoaiTin===
        public entLoaiTin getInfo(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiTinGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Loai_tin_id", DbType.Int32);
            sqla[0].Value = Id;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entLoaiTin entLT = new entLoaiTin();
            if (dr.Read())
            {
                entLT.Loai_tin_id = Convert.ToInt32(dr["Loai_tin_id"].ToString());
                entLT.Ten = dr["Ten"].ToString();
                entLT.So_tin_hien_thi = Convert.ToByte(dr["So_tin_hien_thi"].ToString());
                entLT.Nguoi_tao = Convert.ToInt32(dr["Nguoi_tao"].ToString());
                entLT.Ngay_tao = dr["Ngay_tao"].ToString();
                entLT.Nguoi_sua = Convert.ToInt32(dr["Nguoi_sua"].ToString());
                entLT.Ngay_sua = dr["Ngay_sua"].ToString();
                entLT.Status = Convert.ToByte(dr["Status"].ToString());

            }
            dr.Close();
            sqlConnection.Close();
            return entLT;
        }
        #endregion

        #region === xoa LoaiTin===
        public void delete(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiTinDeleteById", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion
    }
}
